package com.qipay.admin.tags;

import org.springframework.util.StringUtils;

import javax.servlet.jsp.JspException;
import javax.servlet.jsp.JspWriter;
import javax.servlet.jsp.tagext.TagSupport;
import java.io.IOException;

/**
 * Created by dong on 2015/11/5.
 */
public class PageTag extends TagSupport {

    private int pageNo;
    private int pageSize = 10;
    private Long totalSum;
    private String url;
    private String pageName = "p";
    private String pageSizeName = "pz";
    private int isPhone = 0;

    public String getPageSizeName() {
        if (StringUtils.isEmpty(pageSizeName)) {
            return "pz";
        }
        return pageSizeName;
    }

    public void setPageSizeName(String pz) {
        this.pageSizeName = pz;
    }

    public String getPageName() {
        if (StringUtils.isEmpty(pageName)) {
            return "p";
        }
        return pageName;
    }

    public void setPageName(String pageName) {
        this.pageName = pageName;
    }

    public int getPageNo() {
        return pageNo;
    }

    public void setPageNo(int pageNo) {
        this.pageNo = pageNo;
    }

    public int getPageSize() {
        return pageSize;
    }

    public void setPageSize(int pageSize) {
        this.pageSize = pageSize;
    }

    public Long getTotalSum() {
        return totalSum;
    }

    public void setTotalSum(Long totalSum) {
        this.totalSum = totalSum;
    }

    /**
     * 让totalSum支持integer
     * @param totalSum
     */
    public void setTotalSum(Integer totalSum) {
        this.totalSum = totalSum.longValue();
    }


    public String getUrl() {
        return url;
    }

    public void setUrl(String url) {
        this.url = url;
    }

    public int getIsPhone() {
        return isPhone;
    }

    public void setIsPhone(int isPhone) {
        this.isPhone = isPhone;
    }

    @Override
    public int doEndTag() throws JspException {

        JspWriter out = pageContext.getOut();
        try {
            out.print(NumberButton(getPageNo(), getPageSize(), getTotalSum()));
        } catch (IOException ex) {
            ex.printStackTrace();
        }
        return super.doEndTag();
    }

    @Override
    public void release() {
        url = null;
        pageNo = 1;
        totalSum = 0L;
        pageSize = 10;
        super.release();
    }

    private String GetPageUrl(int page) {
        String urlinner = getUrl();
        if (urlinner.indexOf(pageName + "=") > -1) {
            urlinner = urlinner.replaceAll(pageName + "=(\\d*?)(&|$)", pageName + "=" + page + "$2");
            urlinner = urlinner.replaceAll(pageSizeName + "=(\\d*?)(&|$)", pageSizeName + "=" + pageSize + "$2");
            return urlinner;
        } else {
            if (urlinner.indexOf("?") > -1)
                return urlinner + "&" + pageName + "=" + page + "&" + pageSizeName + "=" + pageSize;
            else
                return urlinner + "?" + pageName + "=" + page + "&" + pageSizeName + "=" + pageSize;
        }
    }

    private String NumberButton(int pageIndex, int pageSize, Long recordCount) {
        /*
        * <div class="pages">
            <a href="#"><button type="button" class="btn btn-default">Previous</button></a>
            <a href="#"><button type="button" class="btn btn-default active">1</button></a>
            <a href="#"><button type="button" class="btn btn-default">2</button></a>
            <a href="#"><button type="button" class="btn btn-default">3</button></a>
            <a href="#"><button type="button" class="btn btn-default">4</button></a>
            <a href="#"><button type="button" class="btn btn-default">5</button></a>
            <a href="#"><button type="button" class="btn btn-default">Next</button></a>
        </div>
        * */
        if (pageIndex == 0) {
            pageIndex = 1;
        }

        if (recordCount <= 0)
            return "";

        if (pageSize <= 0)
            return "";
        int cPage = recordCount.intValue() / pageSize;  //总页数

        if (recordCount % pageSize != 0)
            cPage += 1;
        //当前页大于总页数时跳出
        if (cPage < 2)
            return JustOne();
        if (pageIndex > cPage)
            pageIndex = cPage;
        StringBuilder sb = new StringBuilder();
        sb.append("<ul class=\"pagination\">");

        if (pageIndex > 1)
            sb.append("<li class=\"paginate_button previous\"><a href=\"" + GetPageUrl(pageIndex - 1) + "\" >上一页</a></li>");
        else
            sb.append("<li class=\"paginate_button previous disabled\"><a href=\"javascript:\" >上一页</a></li>");

        int prePage = 5;
        int nextPage = 5;
        if (isPhone == 1) {
            prePage = 1;
            nextPage = 2;
            if (pageIndex <= 1)
                nextPage = 3;
        }
        int startPage = pageIndex - prePage;
        int endPage = pageIndex + nextPage;
        if (startPage < 1)
            startPage = 1;
        if (endPage > cPage)
            endPage = cPage;

        for (int i = startPage; i <= endPage; i++) {
            if (i == pageIndex) {
                sb.append("<li class=\"paginate_button active\"><a href=\"javascript:\">"+ i +"</a></li>");
            } else {
                sb.append("<li class=\"paginate_button \"><a href=\"" + GetPageUrl(i) + "\" >" + i + "</a></li>");
            }
        }

        if (pageIndex == cPage)
            sb.append("<li class=\"paginate_button next disabled\"><a href=\"javascript:\">下一页</a></li>");
        else
            sb.append("<li class=\"paginate_button next\"><a href=\""+ GetPageUrl(pageIndex + 1) +"\">下一页</a></li>");

        sb.append("</ul>");
        return sb.toString();
    }

    private String JustOne() {
        return "<ul class=\"pagination\">\n" +
                "        <li class=\"paginate_button previous disabled\"><a href=\"javascript:\">上一页</a></li>\n" +
                "        <li class=\"paginate_button active\"><a href=\"javascript:\">1</a></li>\n" +
                "        <li class=\"paginate_button next disabled\"><a href=\"javascript:\">下一页</a></li>\n" +
                "    </ul>";
    }
}
